/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

/**
 * BindableSelector is given a base bindable with bindable fields
 * Search can be made on these fields
 * When a bindable is selected then the BindableSelected is invoked 
 */

import blueprint4j.Constants;
import blueprint4j.utils.*;

import java.awt.event.*;
import java.awt.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.text.*;

public class BindableSelector extends JPanel implements ActionListener,ListSelectionListener {
   private static final ImageIcon search_icon;

   static {
      ImageIcon ig=null;
      try {
         ig=Gui.loadIconsFromResource(new String[]{Constants.ICON_BASE+"actions/search.png"})[0];
      }
      catch (Exception exception) {
         Log.critical.out(exception);
         System.exit(-1);
      }
      search_icon=ig;
   }

   private JButton search = new JButton("Search",search_icon);
   private BindField fields[] = null;
   private JList main_list = new JList();
   private JPanel list_container = new JPanel();
   private Bindable base = null;
   private Binder binder = new Binder();

   public BindableSelector(Bindable p_base,BindField p_fields[],BindableSelected bindable_selected) 
   throws BindException {
      base = p_base;
      fields = p_fields;
      JPanel main = new JPanel();
      main.setLayout(new GridLayout(fields.length,2));
      for (int i = 0;i < fields.length;i++) {
         main.add(binder.createDataPair(fields[i]).getComponent());
      }
      JPanel header = new JPanel();
      header.setLayout(new GridLayout(1,2));
      JLabel label = new JLabel("Search");
      label.setFont(new Font("Arial", Font.BOLD, 20));
      list_container.add(main_list);
      header.add(label);
      header.add(search);
      add(header);
      add(main);
      add(list_container);

      search.addActionListener(this);
      main_list.addListSelectionListener(this);
   }

   public void actionPerformed(ActionEvent event) {
      try {
         binder.intoBindings();
         //Bindable bindables = base.find(fields,true,true);
         Bindable bindables = null;
         if (bindables != null) {
            list_container.removeAll();
            main_list = new JList(bindables.getListOfAllBindables().getVector());
            main_list.addListSelectionListener(this);
            list_container.add(main_list);
         } else {
            JOptionPane.showMessageDialog(this,"Your search returned no results","",JOptionPane.INFORMATION_MESSAGE);
         }

      } catch (Exception exception) {}
   }

   public void valueChanged(ListSelectionEvent e) {
      try {
         //bindable_selected.bindableSelected((Bindfable)e.getSource());
      }
      catch (Exception exception) {
         exception.printStackTrace();
      }
   }

}

